/**
 *
 */
package com.btkjgc.service.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.util.StringUtils;

import com.btkjgc.models.extend.AdminUserModel;
import com.btkjgc.service.intf.BaseService_Intf;
import com.btkjgc.utils.DateUtils;
import com.btkjgc.utils.HibernateUtil;

/**
 * @author lzh
 */
public class BaseService_Impl implements BaseService_Intf 
{
	@Autowired
	private   SessionFactory sessionFactory; 
 
	private DataSourceTransactionManager transactionManager;  
    
	@Autowired
	private  HttpSession session;
	
	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory=sessionFactory;
	}

 

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}



	
	public int getCount(String sql, List<Object[]> paramList) {
		// TODO Auto-generated method stub
		 sql = "select count(*) FROM ("+sql+") a WHERE 1=1 ";
		int count = 0;
 
		List<Object> param2List=new ArrayList<>();
		for (Object[] params : paramList) {
			 if(params[1].equals("like")){
				 if(!StringUtils.isEmpty( params[2])){
					 param2List.add("%"+ params[2]+"%");
				 }
			 }else {
					if(!StringUtils.isEmpty( params[2])){
						param2List.add( params[2]);
					}
			 }
		}
		Object temp[] = new Object[param2List.size()];
		param2List.toArray(temp);
		count = HibernateUtil.executeSQLQueryInt(sql, temp);
		return count;
	}

	
	public List getList(Class clazz,List<Object[]> paramList,String sql, Integer pageIndex,
			Integer pageSize,String sortfield) {
 
		List<Object> param2List=new ArrayList<>();
		
		for (Object[] params : paramList) {
			 if(params[1].equals("like")){
				 if(!StringUtils.isEmpty( params[2])){
					 param2List.add("%"+ params[2]+"%");
				 }
			 }else {
					if(!StringUtils.isEmpty( params[2])){
						param2List.add( params[2]);
					}
			 }
		}
 
		Object temp[] = new Object[param2List.size()];
		param2List.toArray(temp);
		
		List  list = HibernateUtil.executeSQLQueryList(sql,
				pageIndex, pageSize, temp,clazz);
		
		
		return list;

	}
	
	

	
	public String getwhereSQL(String sql, List<Object[]> paramList,
			String sortfield) {
		
		for (Object[] params : paramList) {
			 if( params[1].equals("like") &&!StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" like ?";
			 }
			 if( params[1].equals("=") && !StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" = ? ";
			 }
			 if( params[1].equals(">") && !StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" > ?";
			 }
			 if( params[1].equals("<") && !StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" < ?";
			 }
			 if( params[1].equals("d>") && !StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" > date_format(?,'%Y-%m-%d %H:%i:%s')";
			 }
			 if( params[1].equals("d<") && !StringUtils.isEmpty( params[2])){
				 sql += " AND "+ params[0] +" < date_format(?,'%Y-%m-%d %H:%i:%s')";
			 }
		}
 
		 if(!StringUtils.isEmpty(sortfield)){
			 sql += " order by "+sortfield+" desc";
		 }
		return sql;
	}



	
	public String getUpdateHql(Object object) {
		// TODO Auto-generated method stub

		String hql = "update from " + object.getClass().getSimpleName()
				+ "  set ";

		try {
			Field[] field = object.getClass().getDeclaredFields();
			for (int i = 0; i < field.length; i++) { // 遍历所有属性
				String name = field[i].getName(); // 获取属性的名字

				Object value = PropertyUtils.getProperty(object, name); // 获取属性的值
				if (!StringUtils.isEmpty(value)&& !"id".equals(name)) {
					
					if(field[i].getType().getSimpleName().equals("Date")){
						hql = hql + name + " ='" + DateUtils.getCSTtoNumberDate(value.toString()) + "',";
					}else {
						hql = hql + name + " ='" + value + "',";
					}
				
				}
			}
			hql=hql.substring(0,hql.length()-1);
			hql = hql + " where id=" + PropertyUtils.getProperty(object, "id");

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return hql;
	}
	 
	
	public   AdminUserModel getLoginUser() {
		 
		AdminUserModel adminUserModel = (AdminUserModel) session.getAttribute("user");
		
		if(adminUserModel==null){
			adminUserModel = new AdminUserModel();
		}
		
		return adminUserModel;
	}

}
